/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.veditor.view.ev;

import java.util.ResourceBundle;
import offset.nodes.client.veditor.model.ev.DataElementView;
import offset.nodes.client.editor.view.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.text.Element;
import offset.nodes.client.veditor.model.DataModel;

/**
 *
 * @author Walter Lütgenau
 */
public class BooleanValueChooser implements DataElementView {

    DataElementViewContext context = null;
    ResourceBundle bundle = ResourceBundle.getBundle("EditorBundle");
 
    public BooleanValueChooser() {

    }

    /**
     * Calle by VirtualBlock, when a view shall be used to retrieve the new value for a data element.
     * @param context The virtual block and the element
     */
    public void setContext(DataElementViewContext context) {
        try {
            this.context = context;

            // retrieve some basic items used below
            String stringValue = context.getVirtualBlock().getModel().getDocument().getText(
                    context.getElement().getStartOffset(), context.getElement().getEndOffset()-context.getElement().getStartOffset()-1);
            Boolean value = Boolean.valueOf(stringValue);
            String data = context.getVirtualBlock().getModel().getData(context.getElement());
            String description = null;
            if (context.getVirtualBlock().getTemplateByElementPath(data) != null ) {
                description = context.getVirtualBlock().getTemplateByElementPath(data).getDescriptionText();
            }
            if (description == null || description.length() == 0)
                description = java.util.ResourceBundle.getBundle("offset/nodes/client/editor/view/resources/EditorBundle").getString("selectOption");

            int result = JOptionPane.showConfirmDialog(null, description);
            if (result == JOptionPane.YES_OPTION)
                substituteContent(context.getElement(), true);
            else if (result == JOptionPane.NO_OPTION)
                substituteContent(context.getElement(), false);
           } catch (Exception ex) {
            Logger.getLogger(BooleanValueChooser.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    /**
     * Substitute the content of the element with the selected enumerated value
     * @param e The element
     * @param value The value
     * @throws java.lang.Exception
     */
    protected void substituteContent(Element e, boolean value) throws Exception {
        StringBuffer buf = new StringBuffer();
        DataModel model = context.getVirtualBlock().getModel();
        
        buf.append(model.getStartTag(e));
        if (value)
            buf.append(Boolean.TRUE.toString());
        else
            buf.append(Boolean.FALSE.toString());
        buf.append(model.getEndTag(e));
        
        model.setOuterHTML(e, buf.toString());
    }

}
